﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chessLogic
{
    public enum MoveType
    {
        Normal, 
        KingCastling, 
        QueenCastling, 
        Passant, 
        Taking
    }
    public class FigureMove
    {
        public MoveType MovingType { get; set; }
        public Position MovingWhere { get; set; }
        public Figure MovingFigure { get; set; }
        public Figure MovingFigurePromotion { get; set; }
        public char MovingAdditChar { get; set; }

        public void ConvertNotation(string notation,  Board board, FigureColor mColor)
        {
            MovingFigurePromotion = null;
            notation = RemoveNotNeedElemens(notation);
            setMovingFigure(notation, mColor);
            if (notation == "O-O")
            {
                MovingType = MoveType.KingCastling;
                return;
            }
            else if (notation == "O-O-O")
            {
                MovingType = MoveType.QueenCastling;
                return;
            }
            else if ((notation.Length == 3 && Char.IsLower(notation[0])) || (notation.Length == 5 && !Char.IsDigit(notation[4])))
            {
                setMovingFigurePromotion(notation, mColor);
                if (notation.Contains("x"))
                    MovingType = MoveType.Taking;
                else
                    MovingType = MoveType.Normal;
            }
            else if (notation.Length == 2 || notation.Length == 3)
                MovingType = MoveType.Normal;
            else if (notation.Length == 4 && notation.Contains("x"))
            {
                setMovingAdditChar(notation);
                MovingType = MoveType.Taking;
                if (!Char.IsUpper(notation[0]))
                {
                    string temp = notation.Remove(0, 2);
                    if (board.IsPositionEmpty(new Position(temp)) && CanPassant( board, new Position(temp)))
                        MovingType = MoveType.Passant;
                }
            }
            else if ((notation.Length == 4 && !notation.Contains("x")))
                MovingType = MoveType.Normal;
            else
                MovingType = MoveType.Taking;
            setMovingWhere(notation);
            setMovingAdditChar(notation);
        }
        public void setMovingFigure(string notation, FigureColor mColor)
        {
            if (notation.Contains("O-O"))
                MovingFigure = new King(mColor);
            else
                MovingFigure = ConvertFromChar(notation[0], mColor);
        }
        public void setMovingFigurePromotion(string notation, FigureColor mColor)
        {
            MovingFigurePromotion = ConvertFromChar(notation[notation.Length - 1], mColor);
        }
        public void setMovingWhere(string notation)
        {
            notation = RemoveNotNeedElemens(notation);
            int index = notation.Length - 2;    
            if (!Char.IsDigit(notation[notation.Length - 1]))
                index--;
            MovingWhere = new Position(notation[index], Int32.Parse(notation[index + 1].ToString()) - 1);
        }
        public void setMovingAdditChar(string notation)
        {
            if (!Char.IsUpper(notation[0]))
                MovingAdditChar = notation[0];
            else if (notation.Length == 5 || (notation.Length == 4 && !notation.Contains("x")))
                MovingAdditChar = notation[1];
            else
                MovingAdditChar = ' ';
        }

        private Figure ConvertFromChar(char ch, FigureColor mColor)
        {
            switch (ch)
            {
                case 'R':
                    return new Rook(mColor);
                case 'N':
                    return new Knight(mColor);
                case 'B':
                    return new Bishop(mColor);
                case 'Q':
                    return new Queen(mColor);
                case 'K':
                    return new King(mColor);
                default:
                    return new Pawn(mColor);
            }
        }
        private string RemoveNotNeedElemens(string notation)
        {
            if (notation.Contains("+") || notation.Contains("#"))
                return notation.Remove(notation.Length - 1);
            return notation;
        }
        private bool CanPassant( Board board, Position position)
        {
            if (MovingFigure.Color == FigureColor.White)
                position.setPosition(position.Letter, position.Number - 1);
            else
                position.setPosition(position.Letter, position.Number + 1);
            return !board.IsPositionEmpty(position);
        }
    }
}
